'''
5/9完成，可以实现从midi文件及对应的wav文件自动提取信息，生成数据集
将文件按照对应的模式放在文件夹下，运行该程序，等待，即可获取数据集
'''

import os
import cwt_fast as cwt
import time_extract_2 as score_ext
import data_set_gen as gen

root_dir = os.getcwd()

scores_lis =[]
for item in os.listdir():
    if os.path.isdir(item):
        scores_lis.append(item)

for score_dir in scores_lis:
    score_name = ''
    score_file = None
    sound_lis = []
    freqinfo_lis = []
    os.chdir(score_dir)

    for item in os.listdir():
        name, ext = os.path.splitext(item)
        if ext == '.mid':
            score_name = name
            score_file = item
            break
    score_ext.main(score_file, score_name+'.py')
    print('-'*30)

    for item in os.listdir():
        name, ext = os.path.splitext(item)
        if ext == '.py' and name == score_name:
            score_file = item
            break

    for item in os.listdir():
        name, ext = os.path.splitext(item)
        if ext == '.wav':
            sound_lis.append(item)

    for sound_file in sound_lis:
        name, _ = os.path.splitext(sound_file)
        cwt.main(sound_file, res_file_name=name+'_freqinfo.py', do_fig_show=0, prt_hint=0)
        gen.main(freq_file_name=name+'_freqinfo.py', time_file_name=score_file, auto_instrument_type=1, output_py_name=name)
        print('-'*30)

    os.chdir(root_dir)

